package xyz.fuqufyb.aec.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import xyz.fuqufyb.aec.dao.AdminUserMapper;
import xyz.fuqufyb.aec.data.JsonResult;
import xyz.fuqufyb.aec.entity.AdminUser;
import xyz.fuqufyb.aec.service.IAdminUserService;

import java.util.List;
import java.util.Map;

/**
 * @author 用户: fyb
 * @description class: AdminUserServiceImpl
 * @date 时间: 2021/1/28 21:30
 */
@Service("IAdminUserService")
public class AdminUserServiceImpl implements IAdminUserService {

    @Autowired
    private AdminUserMapper adminUserMapper;

    @Override
    public JsonResult userLogin(String email, String password) {
        /**
         * 用户名和密码不能为空
         */

        if (StringUtils.isEmpty(email) && StringUtils.isEmpty(password)) {
            return JsonResult.getJsonError("用户名和密码不能为空");
        }

        AdminUser adminUser = adminUserMapper.selectByEmail(email);
        /**
         * 先判断当前用户存不存在
         * 用户名或密码不正确
         */
        if (adminUser == null || !adminUser.getPassword().equals(password)) {

            return JsonResult.getJsonError("用户名或密码不正确");
        }
        return JsonResult.getJsonSuccess(adminUser);
    }

    @Override
    public int deleteByPrimaryKey(Integer id) {
        int i = adminUserMapper.deleteByPrimaryKey(id);
        if (i > 0) {
            return i;
        }
        return 0;
    }

    @Override
    public int insert(AdminUser record) {

        int insert = adminUserMapper.insert(record);
        if (insert > 0) {

            return insert;
        }
        return 0;
    }

    @Override
    public int insertSelective(AdminUser record) {
        int i = adminUserMapper.insertSelective(record);
        if (i > 0) {
            return i;
        }
        return 0;
    }

    @Override
    public AdminUser selectByPrimaryKey(Integer id) {

        AdminUser adminUser = adminUserMapper.selectByPrimaryKey(id);
        if (adminUser == null) {
            return null;
        }
        return adminUser;
    }

    @Override
    public int updateByPrimaryKeySelective(AdminUser record) {

        int i = adminUserMapper.updateByPrimaryKeySelective(record);

        if (i > 0) {
            return i;
        }
        return 0;
    }

    @Override
    public int updateByPrimaryKey(AdminUser record) {
        int i = adminUserMapper.updateByPrimaryKey(record);
        if (i > 0) {
            return i;
        }
        return 0;
    }

    @Override
    public JsonResult selectUserList(Map<String, Object> maps) {

        PageHelper.startPage(Integer.parseInt((String) maps.get("page")),Integer.parseInt((String)  maps.get("limit")));
        List<AdminUser> adminUsers = adminUserMapper.selectUserList(maps);

        if (!adminUsers.isEmpty()) {
            PageInfo<AdminUser> pageInfo = new PageInfo<>(adminUsers);
            return JsonResult.getJsonSuccess(pageInfo);
        }
       return null;
    }

    @Override
    public int deleteByUserIds(List<Integer> asList) {
       int i =  adminUserMapper.deleteByUserIds(asList);
        if (i>0){
            return i;
        }
        return 0;
    }
}
